package io.renren.modules.employment.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.renren.common.annotation.Login;
import io.renren.common.config.ListPageUtils;
import io.renren.modules.employment.entity.*;
import io.renren.modules.sys.dto.PostionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.common.utils.R;
import io.renren.modules.employment.service.PostionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/postion")
@Api(tags="就业接口2")
public class PostionController {
		
	@Autowired
	PostionService postionService;
	
	//查询所有的岗位信息
	@PostMapping("getAllPostion")
	@ApiOperation("获取岗位列表")
	public R getAllPostion(@RequestBody Map params) {
		int pageNum = Integer.parseInt(params.get("pageNum").toString());
		int pageSize = Integer.parseInt(params.get("pageSize").toString());
		params.put("pageNum",pageNum);
		params.put("pageSize",pageSize);
		List<CompanyAndPostion> postionList = postionService.getAllPostion();
		if(postionList.size()>0){
			ListPageUtils pageUtil = new ListPageUtils(postionList,pageSize);
			List list = pageUtil.getPagedList(pageNum);
			if(list.size() > 0){
				return R.ok("查询岗位成功",100).put("data",list).put("nextNum",pageNum+1);
			}
			return R.ok("查询岗位成功",100).put("data",list).put("nextNum",-1);
		}
		return R.ok("查询成功！",100).put("data",postionList).put("nextNum",-1);
	}
	
	//根据当前岗位id查询当前岗位
	@GetMapping("getPostionById")
	@ApiOperation("获取一个岗位信息")
	public R getPostionById(@RequestParam String id) {
		
		Position postion = postionService.getPostionById(id);
		
		return R.ok("",100).put("data",postion);
	}
	
	//根据当前岗位id查询当前公司id 展示公司信息的接口
	@PostMapping("getCompanyByPostion2")
	@ApiOperation("获取一个岗位信息的所属公司")
	public R getCompanyByPostion2Id(@RequestBody Postion2 postion) {  //传的是岗位的id
		Company company = postionService.getCompanyByPostion2(postion);
		return R.ok("",100).put("data",company);
	}
	
	//根据公司查询除本岗位的其他岗位信息
	@PostMapping("getPostionNotIncludeThisPostionByCompany")
	@ApiOperation("查询本公司的其他职位")
	public R getPostionNotIncludeThisPostionByCompany(@RequestBody PostionDto postion) {
		int pageNum = postion.getPageNum();
		int pageSize = postion.getPageSize();
		List postionList = postionService.getPostionNotIncludeThisPostionByCompany(postion);
		if(postionList.size()>0){
			ListPageUtils pageUtil = new ListPageUtils(postionList,pageSize);
			List list = pageUtil.getPagedList(pageNum);
			if(list.size() > 0){
				return R.ok("查询岗位成功",100).put("data",list).put("nextNum",pageNum+1);
			}
			return R.ok("本公司未发布其他岗位",100).put("data",list).put("nextNum",-1);
		}
		return R.ok("查询岗位成功！",100).put("data",postionList).put("nextNum",-1);
	}
	
	//根据传来的岗位id查询福利
	@PostMapping("getWelfareByPostionId")
	@ApiOperation("根据传来的岗位id查询福利")
	public R getWelfareByPostionId(@RequestBody Postion2 postion) {
		List<Welfare> welfareList = postionService.getWelfareByPostionId(postion);
		
		return R.ok("",100).put("data",welfareList);
	}

	/**
	 * 对职位的模糊查询
	 */
	@PostMapping("getPostionByLike")
	@ApiOperation("对职位的模糊查询")
	public R getPostionByLike(@RequestBody Map params){
		String postionTitle = params.get("postionTitle").toString();
		int pageNum = Integer.parseInt(params.get("pageNum").toString());
		int pageSize = Integer.parseInt(params.get("pageSize").toString());
		params.put("pageNum",pageNum);
		params.put("pageSize",pageSize);
		List<Postion2> postionList = postionService.getPostionByLike(postionTitle);
		if(postionList.size()>0){
			ListPageUtils pageUtil = new ListPageUtils(postionList,pageSize);
			List list = pageUtil.getPagedList(pageNum);
			if(list.size() > 0){
				return R.ok("查询职位成功",100).put("data",list).put("nextNum",pageNum+1);
			}
			return R.ok("没有符合条件的职位哦~",100).put("data",list).put("nextNum",-1);
		}
		return R.ok("查询职位成功！",100).put("data",postionList).put("nextNum",-1);
	}


	/**
	 * 获取两层岗位列表
	 */
	@PostMapping("getPosts")
	@Login
	public R getPosts(@RequestAttribute("userId") Long userId, @RequestBody Map map){
		map.put("userId",userId);
		List postList = postionService.getPosts(map);
		return R.ok("",100).put("data",postList);
	}

	/**
	 * 记录用户选择的岗位
	 */
	@PostMapping("insertUserPosts")
	@Login
	public R insertUserPosts(@RequestAttribute("userId") Long userId, @RequestBody Map map){
		map.put("userId",userId);
		PostUser postUser =postionService.getUserPosts(userId);
		if(null == postUser || "".equals(postUser.getPostId())){
			postionService.insertUserPosts(map);
		}
		else{
			postionService.updateUserPosts(map);
		}
		return R.ok("",100);
	}


	/**
	 * 获取用户选择的岗位
	 */
	@GetMapping("getUserPosts")
	@Login
	public R getUserPosts(@RequestAttribute("userId") Long userId){
		PostUser postUser =postionService.getUserPosts(userId);
		return R.ok("",100).put("data",postUser);
	}

	/**
	 * 根据岗位id获取岗位
	 */
	@GetMapping("getUserPostById")
	public R getUserPostById(@RequestAttribute("postId") int postId){
		PostAndIndustry2 postUser =postionService.getUserPostById(postId);
		return R.ok("",100).put("data",postUser);
	}
}
